<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2020-11-14 18:04:32
 * @Last Modified by:   lianlianzan(13040@qq.com)
 * @Last Modified time: 2020-12-12 10:34:27
 */

namespace app\mobmall_manage\controller;
use app\common\controller\Backend;

class Slide extends Backend {

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '图片轮播管理', '', 'slide');

		$this->slideDao = model('Slide');
	}

	public function index() {
		$map = $parameter = array();

		$count = $this->slideDao->countSearch($map);
		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		$pageShow = $Page->show();
		list($list) = $this->slideDao->search($map, $Page->getLimit());
		//print_r($list);exit;
		$this->assign('count', $count);
		$this->assign('pageShow', $pageShow);
		$this->assign('list', $list);

		return $this->fetch();
	}

	public function listorders() {
		$result = parent::baseListorders($this->slideDao);
		if ($result !== false) {
			$this->success('排序更新成功');
		} else {
			$this->error('排序更新失败');
		}
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->slideDao->getInfo($id);
		} else {
			$info = [
				'status' => 1
			];
		}
		$this->assign('id', $id);
		$this->assign('info', $info);
		
		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		if ($id) {
			$info = $this->slideDao->get($id);
		}

		//普通上传图片
		list($uploadInfo, $isDelOldImg) = $this->comuploadFile('slide', 'picurl', $info['picurl']);
		$uploadInfo && $data['picurl'] = $uploadInfo['saveFullPath'];

		//验证数据
		// $validate = validate('slide');
		// if (!$validate->check($data)) {
		//     $this->error($validate->getError());
		// }

		if ($id) {
			$result = $this->slideDao->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->clearCache(); //清除缓存
				$this->success('保存成功', url('mobmall_manage/slide/index'));
			} else {
				$this->error('保存失败'.showDbError($this->slideDao));
			}
		} else {
			$data['create_time'] = $this->nowtime;
			$result = $this->slideDao->baseAddData($data);
			if ($result !== false) {
				$this->clearCache(); //清除缓存
				$this->success('添加成功', url('mobmall_manage/slide/index'));
			} else {
				$this->error('添加失败'.showDbError($this->slideDao));
			}
		}
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');

		//获取信息(不解析)
		$info = $this->slideDao->get($id);

		//删除对应数据库信息
		$result = $this->slideDao->delInfo($id);

		//删除图片文件
		$info['picurl'] && $this->delFtpFile($info['picurl']);

		if ($result !== false) {
			$this->clearCache(); //清除缓存
			$this->success('删除成功', url('mobmall_manage/slide/index'));
		} else {
			$this->error('删除失败'.showDbError($this->slideDao));
		}
	}

	private function clearCache() {
		$sys_identify = 'slide';
		cache($sys_identify, NULL); //删除缓存数据
	}
	
}